<template>
  <a-card :bordered="false">
    <div class="table-page-search-wrapper">
      <a-form layout="inline">
        <a-row :gutter="48">
          <a-col :md="8" :sm="24">
            <a-form-item label="部别名称">
              <a-input v-model="queryParam.unitName" placeholder=""/>
            </a-form-item>
          </a-col>
          <a-col :md="!advanced && 8 || 24" :sm="24">
            <span class="table-page-search-submitButtons" :style="advanced && { float: 'right', overflow: 'hidden' } || {} ">
              <a-button type="primary" @click="$refs.table.refresh(true)">查询</a-button>
              <a-button style="margin-left: 8px" @click="() => queryParam = {}">重置</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <div class="table-operator">
      <a-button type="primary" icon="plus" @click="$refs.addUnit.add()">新建</a-button>
<!--      <a-button type="dashed" @click="tableOption">{{ optionAlertShow && '关闭' || '开启' }}提示</a-button>-->
      <a-dropdown v-action:edit v-if="selectedRowKeys.length > 0">
        <a-menu slot="overlay">
          <a-menu-item key="remove">
            <remove-unit ref="removeUnit" :unitIds="selectedRowKeys.join(',')" @ok="handleOk"/>
          </a-menu-item>
        </a-menu>
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
      <a-dropdown  v-action:edit v-else-if="selectedRowKeys.length == 0">
        <a-button style="margin-left: 8px">
          批量操作 <a-icon type="down" />
        </a-button>
      </a-dropdown>
    </div>

    <s-table
      ref="table"
      size="default"
      rowKey="key"
      :columns="columns"
      :data="loadData"
      :alert="options.alert"
      :rowSelection="options.rowSelection"
      showPagination="auto"
    >
      <span slot="unitType" slot-scope="text">
        <a-tag :color="text | unitTypeColorFilter">{{ text | unitTypeTextFilter }}</a-tag>
      </span>
      <span slot="unitFather" slot-scope="text">
        <a-tag color="#108ee9">{{ text | unitFatherTextFilter }}</a-tag>
      </span>
      <span slot="action" slot-scope="text, record">
        <template>
          <a @click="handleEdit(record)">编辑</a>
          <a-divider type="vertical" />
          <remove-unit ref="removeUnit" :unitIds="String(record.unitId)" @ok="handleOk"/>
        </template>
      </span>
    </s-table>
    <add-unit ref="addUnit" :unitFathers="unitFathers" @ok="handleOk" />
    <edit-unit ref="editUnit" :unitFathers="unitFathers" @ok="handleOk"/>
  </a-card>
</template>

<script>
// import moment from 'moment'
import { STable } from '@/components'
import { queryUnit, listUnit } from '@/api/basic/unit'
import AddUnit from './modules/unit/AddUnit'
import EditUnit from './modules/unit/EditUnit'
import RemoveUnit from './modules/unit/RemoveUnit'
// 用于处理过滤器动态获取数据
let that

const unitTypeMap = {
  1: {
    color: 'orange',
    text: '旅'
  },
  2: {
    color: 'green',
    text: '营/部'
  },
  3: {
    color: 'cyan',
    text: '连/科'
  }
}

export default {
  name: 'UnitList',
  components: {
    STable,
    AddUnit,
    EditUnit,
    RemoveUnit
  },
  data () {
    return {
      // 高级搜索 展开/关闭
      advanced: false,
      // 查询参数
      queryParam: {},
      // 部别对象
      unitFathers: [],
      // 表头
      columns: [
        {
          title: '部别名称',
          dataIndex: 'unitName',
          align: 'center'
        },
        {
          title: '上级部别',
          dataIndex: 'unitFather',
          scopedSlots: { customRender: 'unitFather' },
          align: 'center'
        },
        // {
        //   title: '备注信息',
        //   dataIndex: 'unitInfo',
        //   align: 'center'
        // },
        {
          title: '部别类型',
          dataIndex: 'unitType',
          scopedSlots: { customRender: 'unitType' },
          align: 'center'
        },
        {
          title: '操作',
          dataIndex: 'action',
          align: 'center',
          scopedSlots: { customRender: 'action' }
        }
      ],
      // 加载数据方法 必须为 Promise 对象
      loadData: parameter => {
        // 统一处理查询参数，便于后端表格DTO用固定参数接收
        const queryJson = { 'queryParam': JSON.stringify(this.queryParam) }
        return queryUnit(Object.assign(parameter, queryJson))
          .then(res => {
            return res.result
          })
      },
      selectedRowKeys: [],
      selectedRows: [],

      options: {
        alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
        rowSelection: {
          selectedRowKeys: this.selectedRowKeys,
          onChange: this.onSelectChange
        }
      },
      optionAlertShow: true
    }
  },
  filters: {
    unitTypeTextFilter (type) {
      return unitTypeMap[type].text
    },
    unitTypeColorFilter (type) {
      return unitTypeMap[type].color
    },
    unitFatherTextFilter (unitId) {
      let unitFatherName = ''
      for (const unit of that.unitFathers) {
        if (parseInt(unit.unitId) === parseInt(unitId)) {
          unitFatherName = unit.unitName
          break
        }
      }
      return unitFatherName
    }
  },
  beforeCreate () {
    that = this
  },
  created () {
    // this.tableOption()
    listUnit()
      .then(res => {
        if (res.message !== 'error') {
          this.unitFathers = res.result
        } else {
          this.$message.error('操作失败！')
        }
      })
  },
  methods: {
    // tableOption () {
    //   if (!this.optionAlertShow) {
    //     this.options = {
    //       alert: { show: true, clear: () => { this.selectedRowKeys = [] } },
    //       rowSelection: {
    //         selectedRowKeys: this.selectedRowKeys,
    //         onChange: this.onSelectChange
    //       }
    //     }
    //     this.optionAlertShow = true
    //   } else {
    //     this.options = {
    //       alert: false,
    //       rowSelection: null
    //     }
    //     this.optionAlertShow = false
    //   }
    // },
    handleEdit (record) {
      this.$refs.editUnit.edit(record)
    },
    handleOk () {
      this.$refs.table.clearSelected()
      this.$refs.table.refresh(false)
    },
    onSelectChange (selectedRowKeys, selectedRows) {
      this.selectedRowKeys = selectedRowKeys
      this.selectedRows = selectedRows
    },
    toggleAdvanced () {
      this.advanced = !this.advanced
    }
  }
}
</script>
